<template>
  <!-- 我的-->
  <div>
    <!-- 登录状态头部 -->
    <div v-if="user" class="header user-info">
      <div class="base-info">
        <div class="left">
          <van-image class="user-img" :src="userInfo.photo" round fit="cover" />
          <span class="name">{{ userInfo.name }}</span>
        </div>
        <div class="right">
          <van-button
            to="/user/profile"
            class="ziliao"
            type="default"
            round
            size="mini"
            >编辑资料</van-button
          >
        </div>
      </div>
      <div class="data-stats">
        <div class="data-item">
          <p>{{ userInfo.art_count }}</p>
          <span>头条</span>
        </div>
        <div class="data-item">
          <p>{{ userInfo.follow_count }}</p>
          <span>关注</span>
        </div>
        <div class="data-item">
          <p>{{ userInfo.like_count }}</p>
          <span>粉丝</span>
        </div>
        <div class="data-item">
          <p>{{ userInfo.fans_count }}</p>
          <span>获赞</span>
        </div>
      </div>
    </div>
    <!-- /登录状态头部 -->

    <!-- 未登录状态头部 -->
    <div v-else class="header not-login">
      <div class="login-btn" @click="$router.push('/login')">
        <img src="~@/assets/mobile.png" alt="" />
        <span class="text">登录 / 注册</span>
      </div>
    </div>
    <!-- /未登录状态头部 -->

    <!-- 功能导航 -->
    <van-grid :column-num="2" class="grid-tou" clickable>
      <van-grid-item class="grid-item">
        <i slot="icon" class="toutiao toutiao-shoucang"></i>
        <span slot="text">收藏</span>
      </van-grid-item>
      <van-grid-item class="grid-item">
        <i slot="icon" class="toutiao toutiao-lishi"></i>
        <span slot="text">历史</span>
      </van-grid-item>
    </van-grid>
    <!-- /功能导航 -->

    <van-cell title="消息通知" is-link />
    <van-cell title="小智同学" is-link />

    <van-button v-if="user" type="default" @click="exit" class="exit" clickable>
      退出登录
    </van-button>
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
import { getUserInfo } from "@/api/user.js";
export default {
  created() {
    //   用户登录在获取数据
    if (this.user) {
      this.getUser();
    }
  },
  data() {
    return {
      userInfo: {},
    };
  },
  methods: {
    //   路由公用数据方法
    ...mapMutations(["removeUser"]),
    //   退出登录
    exit() {
      this.$dialog
        .confirm({
          title: "确认退出嘛？",
        })
        .then(() => {
          this.removeUser();
          this.$toast.success({ duration: 300, message: "退出成功" });
        })
        .catch(() => {
          this.$toast.fail({ duration: 300, message: "已取消" });
        });
    },

    // 获取用户信息
    async getUser() {
      try {
        const { data: res } = await getUserInfo();
        this.userInfo = res.data;
      } catch (err) {
        console.log(err);
      }
    },
  },
  computed: {
    ...mapState(["user"]),
  },
};
</script>

<style lang="less" scoped>
.header {
  height: 361px;
  background: url("~@/assets/banner.png");
  background-size: cover;
}

.not-login {
  display: flex;
  justify-content: center;
  align-items: center;

  .login-btn {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    img {
      width: 132px;
      height: 132px;
    }
    span {
      margin-top: 15px;

      font-size: 28px;
      color: #fff;
    }
  }
}

// 登录状态头部
.user-info {
  .base-info {
    height: 231px;
    padding: 76px 32px 23px;
    box-sizing: border-box;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .left {
      display: flex;
      justify-content: center;
      align-items: center;
      .user-img {
        width: 132px;
        height: 132px;
        border: 4px solid #fff;
      }
      .name {
        font-size: 30px;
        color: #fff;
        margin-left: 23px;
      }
    }
    .right {
      .ziliao {
        padding: 0 15px;
        color: #8c8c8c;
      }
    }
  }
  .data-stats {
    box-sizing: border-box;
    height: 130px;
    background: #3e9cfa;
    padding: 32px;
    color: #fff;
    display: flex;
    font-size: 28px;

    justify-content: space-between;
    .data-item {
      display: flex;
      flex: 1;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      p {
        margin-top: 18px;
        font-size: 32px;
        margin: 0;
      }
    }
  }
}
// 登录状态头部

// 功能导航
.grid-tou {
  .grid-item {
    i {
      font-size: 47px;
      color: #eb5253;
    }
    &:last-child i {
      color: #ffae44;
    }
    span {
      margin-top: 15px;
      font-size: 30px;
    }
  }
}
// 功能导航

.exit {
  width: 100%;
  color: #d86262;
}
</style>